﻿using System;
using System.Collections.Generic;
using System.Linq;
using Simple.Data;

namespace Model.Course
{
    public class Lecture
    {
        protected String name;
        protected DateTime created;
        protected String content;
        protected Int32 id;
        protected Int32 courseId;

        protected Lecture()
        {
        }

        protected Lecture(int id, String name, DateTime created, String content, int courseId)
        {
            this.id = id;
            this.name = name;
            this.created = created;
            this.content = content;
            this.courseId = courseId;
        }

        public static Lecture create(Course course, String name, DateTime created, String content)
        {
            var db = Database.Open();

            var lecture = db.Lecture.Insert(Name: name, Content: content, Created: created, 
                                                  Course: course.getId());

            return new Lecture(lecture.id, name, created, content, lecture.course);
        }

        public static Lecture get(int lectureId)
        {
            var db = Database.Open();

            var lecture = db.Lecture.Get(lectureId);

            if (lecture == null) return null;

            return new Lecture(lecture.id, lecture.name, lecture.created, lecture.content, lecture.course);
        }

        public static Lecture[] getLectures(Course course)
        {
            var lecturesRows = Database.Open().Lecture.FindAllByCourse(course.getId());
            int count = Database.Open().Lecture.GetCountByCourse(course.getId());

            Lecture[] lectures = new Lecture[count];

            int i = 0;
            foreach (var t in lecturesRows)
                lectures[i++] = new Lecture(t.id, t.name, t.created, t.content, course.getId());

            return lectures;
        }

        public void delete()
        {
            Database.Open().Lecture.DeleteById(id);
        }

        public int getId()
        {
            return id;
        }

        public String getName()
        {
            return name;
        }

        public void setName(String name)
        {
            this.name = name;
            Database.Open().Lecture.UpdateById(Id: id, Name: name);
        }

        public String getContent()
        {
            return content;
        }

        public void setContent(String content)
        {
            this.content = content;
            Database.Open().Lecture.UpdateById(Id: id, Content: content);
        }

        public DateTime getCreated()
        {
            return created;
        }

        public void setCreated(DateTime created)
        {
            this.created = created;
            Database.Open().Lecture.UpdateById(Id: id, created: created);
        }

        public Attachment[] getAttachments()
        {
            return Attachment.getAttachments(Attachment.OwnerType.Lecture, id);
        }

        /*public void addAttachment(String fileName, String filePath)
        {
            Attachment.create(fileName, filePath, Attachment.OwnerType.Lecture, id);
        }
        */
        public void addAttachment(Attachment att)
        {
            att.setOwnerId(Attachment.OwnerType.Lecture, this.id);
        }
    }
}